package FileControl.Source;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile extends AbstractCopy {
	
	public static int bufSize = 1024;

	/**
	 * 
	 * @param File to copy from, and to
	 * @return true/false if copy was successful
	 */
	public static boolean basicCopy(File source, File dest) {
		boolean success = true;
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(source);
			out = new FileOutputStream(dest);
		    
			 // Transfer bytes from in to out
			 byte[] buf = new byte[bufSize];
			 int len;
			 while ((len = in.read(buf)) > 0) {
				 out.write(buf, 0, len);
			 }
		} catch (FileNotFoundException e) {e.printStackTrace(); success = false;
		} catch (IOException e) {e.printStackTrace(); success = false;
		} finally {
			 if(in != null) {
				 try {
					in.close();
				} catch (IOException e) {e.printStackTrace();}
			 }
			 if(out != null) {
				 try {
					out.close();
				 } catch (IOException e) {e.printStackTrace();}
			 }
		}
		return success;
	}
	
	public static boolean copyDestCheck(File source, File dest) {
		if(dest.exists()) {
			println(source.getName() + ": FILE ALREADY EXISTS!");
			return false;
		} else {
			return basicCopy(source, dest);
		}
	}
}
